<template>
	<view class="root">
		<u-button plain :ripple="true" ripple-bg-color="#909399" @click='sanc' type="primary">扫一扫</u-button>
		<!-- 	<button @click='opne'>打开</button>
		<button @click='clos'>关闭</button> -->

		<!-- #ifdef H5 -->
		<!-- <web-view :src="url" @message="message" :webview-styles="webviewStyles"></web-view> -->
		<!-- #endif -->


		<u-popup v-model="show" mode="center" border-radius="14" height='' width='80%'>
			<view class="title"> 扫描内容</view>
			<view class='content'>扫码类型: {{scan.type}} </view>
			<view class='content'>
				<view>内容: </view>
				<view style="word-break:break-all" :class="{lans:scan.isURL ? true :false}">{{scan.result}}</view>
				<u-tag text="打开" @click='openURL(scan.result)' type="primary" v-show='scan.isURL' mode='dark' />
				<u-tag text="复制" @click='copeFunc(scan.result)' type="primary" />
			</view>
			<view v-show="scan.path">本应用路径: {{scan.path}} </view>
		</u-popup>


		<u-button class="custom-style" v-show='creationWebivew' @click='clos'
			style='position: fixed;bottom: 0px;width: 100%;'>关闭</u-button>
	</view>
</template>

<script>
	export default {
		name: 'sacn',
		data() {
			return {

				url: "http://server1.miaoshare.com:7002/",
				webviewStyles: {
					color: '#FF3333',
					height: '80%'
				},
				show: false,
				nw: '',
				// 扫描内容
				scan: {},
				creationWebivew: '',
			}
		},
		onBackPress() {
			this.clos();
			console.log('返回了返回来');
		},
		onNavigationBarSearchInputConfirmed(e){
			console.log('监听原生标题栏搜索输入框搜索事件，用户点击软键盘上的“搜索”按钮时触发',e);
		},
		methods: {
			async openURL(url='https://image.baidu.com/') {
				console.log(url);
				this.creationWebivew = await this.APPPLUS.webview(url, {
					success(embed) {
						console.log("页面地址为：" + embed.getURL(), embed.getTitle(), embed
							.getTitleNViewSearchInputText());
					},
					loaded() {

					},
					pulldown(embed) {
						embed.loadURL(embed.getURL())
					}
				}, 'selfsummer', {
					top: '0px',
					bottom: '40px'
				})
				
				console.log(this.creationWebivew);
			},
			opne() {
				//#ifdef APP-PLUS
				// const hh = 	plus.webview.open('https://www.baidu.com/',
				// 		'selfsummer', {
				// 			top: '220px',
				// 			bottom: '0px',
				// 			additionalHttpHeaders: {
				// 				Referer: 'http://m.dcloud.io/'
				// 			},
				// 		},
				// 		"slide-in-top",
				// 		1000, {
				// 			popGesture: 'hide',
				// 			subNViews: [{
				// 				id: 'subnview1',
				// 				styles: {
				// 					top: '0px',
				// 					height: '100px',
				// 					backgroundColor: '#FF0000'
				// 				},
				// 				tags: [{
				// 					tag: 'font',
				// 					id: 'font',
				// 					text: '原生子View控件',
				// 					textStyles: {
				// 						size: '18px'
				// 					}
				// 				}]
				// 			}]
				// 		},
				// 		(error) => {
				// 			var code = error.code; // 错误编码
				// 			var message = error.message; // 错误描述信息
				// 		})

				// var ws = null,embed = null;
				//  ws = plus.webview.currentWebview();

				// embed = plus.webview.create('http://m.weibo.cn/u/3196963860', 'selfsummer', {
				// 	top: '220px',
				// 	bottom: '0px'
				// });
				// embed.show();

				// embed.addEventListener('loaded', function() {
				// 	console.log("页面地址为：" + embed.getURL(), embed.getTitle(), embed.getTitleNViewSearchInputText());


				// }, false);
				// embed.onloaded = () => {
				// 	console.log('webview加载完成', embed.onHistoryQuery);


				// };

				// embed.setPullToRefresh({
				// 	support: true,
				// 	height: '50px',
				// 	range: '200px',
				// 	contentdown: {
				// 		caption: '下拉可以刷新'
				// 	},
				// 	contentover: {
				// 		caption: '释放立即刷新'
				// 	},
				// 	contentrefresh: {
				// 		caption: '正在刷新...'
				// 	}
				// }, () => {



				// 	console.log(1111111111111111)
				// 	// embed.loadURL('https://www.baidu.com/');
				// 	embed.endPullToRefresh();
				// });
				// plus.nativeUI.toast('下拉可以刷新');

				// setTimeout(() => {
				// 	// 加载新的地址
				// 	embed.loadURL('https://www.baidu.com/');
				// }, 5000)




				// #endif
			},
			clos() {
				plus.webview.close('selfsummer');
				this.creationWebivew = ''
			},
			copeFunc(data){
				this.APPPLUS.copy(data)
			},
			sanc() {
				// this.openURL()
			if(this.isApp){
				uni.scanCode({
					success: (res) => {
						const {
							scanType,
							path,
							charSet,
							result
						} = res;
						console.log(JSON.stringify(res));
				
						this.scan = {
							type: scanType,
							path,
							result,
							isURL: (result.startsWith('https:') || result.startsWith('http:')) ? true :false
						}
						console.log(this.scan);
						try {
							// if (scanType === 'QR_CODE' && result.startsWith('http://') || result
							// 	.startsWith('https://')) {
							// 	// this.creationWebivew(result)
							// 	console.log('条码内容：' + result, this.url);
							// }
							// this.url = scanData
						} catch (e) {
							//TODO handle the exception
						}
				
						this.show = true;
				
					},
					fail() {
						this.uniFunc.Toast('内容识别错误')
					}
				});
			}else{
				
			}

			},
			isURL(str) {
				if (str.startsWith('https:') || str.startsWith('http:')) {
					try {
						new URL(str)
						return true
					} catch (e) {
						console.log('err', e);
						return false
					}
				} else {
					return false
				}

			},
			message(e) { //接收html发回来的消息判断是否成功，然后跳转页面
				console.log('接收html发回来的消息判断是否成功，然后跳转页面', e.detail)
				//                 uni.switchTab({
				//                     url: '/pages/member/user'
				//                 });
			},
			
		}
	}
</script>

<style scoped lang="less">
	.title {
		text-align: center;
		font-weight: 800;
		font-size: 36rpx;
		padding: 20rpx;
	}

	.content {
		padding: 20rpx;
	}

	.lans {
		color: #007AFF;
	}
</style>
